package com.example.tulingtest;



import com.example.model.Apple;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @Description:
 * @Project:
 * @CreateDate: Created in 2019/3/5 14:11
 * @Author: zjq
 */
public class Testone {

    private static List<Apple> appleStore = new ArrayList<>();

    static {
        appleStore.add(new Apple(1, "black", 12, "nanjing"));
        appleStore.add(new Apple(2, "red", 13, "suzhou"));
        appleStore.add(new Apple(3, "blue", 14, "nantong"));
        appleStore.add(new Apple(4, "pink", 15, "xuzhou"));

    }

    public static void main(String[] args) {
        Testone t = new Testone();
        //lambada 表达式的 传递条件
        List<Apple> list = t.getAppleByPredicate(a -> a.getColor().equalsIgnoreCase("red")&&a.getWeight()>10);
        list.stream().forEach(n -> {
            System.out.println(n);
        });

        System.out.println("==============================================");
        System.out.println("==============================================");
        //打印所有
        appleStore.stream().forEach(System.out::println);
        //过滤 打印 排序要进行空情况
        appleStore.stream().filter(a->a.getColor().equalsIgnoreCase("red"))
                .sorted((o1,o2)->o1.getWeight()-o2.getWeight()).forEach(System.out::print);

    }

    //获取   红色 苹果  原版写法
    public List<Apple> getAppleByColor(String color) {
        List<Apple> list = new ArrayList<>();
        for (Apple a : appleStore) {
            if (a.getColor().equalsIgnoreCase(color)) {
                list.add(a);
            }
        }
        return list;
    }

    //lamba表达式写法 直接传条件逻辑
    public List<Apple> getAppleByPredicate(Predicate<Apple> predicate) {
        List<Apple> list = new ArrayList<>();
        appleStore.stream().forEach(n -> {
            if (predicate.test(n)) {
                list.add(n);
            }
        });
        return list;
    }


}
